package cn.yangliu.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 作者 杨柳
 * 创建时间 2017-08-21 11:33
 */
@Configuration
public class WebConfig {
    private SerializerFeature[] serializerFeatures={SerializerFeature.WriteNullStringAsEmpty,
            SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteNullListAsEmpty};

    /**
     * 使用阿里fastjson转换 不使用jackson
     */
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters(FastJsonConfig fastJsonConfig) {
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        //数据为空展示为空字符串
        fastConverter.setFastJsonConfig(fastJsonConfig);
        return new HttpMessageConverters(fastConverter);
    }


    @Bean
    public FastJsonConfig fastJsonConfig(){
        //数据为空展示为空字符串
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(serializerFeatures);
        return fastJsonConfig;
    }
}
